package com.kuang.controller;

import com.kuang.dao.DepartmentDao;
import com.kuang.dao.EmployeeDao;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;

    @RequestMapping("/emps")
    public String list(Model model) {
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps", employees);
        System.out.println("员工管理系统");
        return "emp/list";
    }

    @GetMapping("/emp")
    public String toAddPage(Model model) {
        //查询所有的部门信息
        Collection<Department> departments = DepartmentDao.getDepartment();
        model.addAttribute("departments", departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String Addemp(Employee employee) {
        System.out.println(employee);
        // 回到员工列表页面，可以使用redirect或者forward，就不会被视图解析器解析
        employeeDao.save(employee);//调用底层方法保存员工
        return "redirect:/emps";
    }

    //从HTML的编辑按钮,跳转到员工的修改页面
    @GetMapping("/emp/{id}")
    public String toUpdatetmp(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp", employee);
        //查询所有的部门信息
        Collection<Department> departments = DepartmentDao.getDepartment();
        model.addAttribute("departments", departments);
        return "emp/update";
    }
    //删除员工
    @GetMapping("/delEmp/{id}")
    public String delEmp(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }
}

